{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>目录<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#需求\" data-toc-modified-id=\"需求-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>需求</a></span></li><li><span><a href=\"#思想\" data-toc-modified-id=\"思想-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>思想</a></span></li><li><span><a href=\"#实现\" data-toc-modified-id=\"实现-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>实现</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 需求\n",
    "1. 比较两个软件版本号的大小,eg:1.2.3a < 1.2.3b\n",
    "1. 不能通过系统提供的比较函数\n",
    "1. 考虑各种情况:\n",
    "    1. 为空\n",
    "    1. 长度不一\n",
    "    1. 分割后的item情况:\n",
    "        1. 全数字\n",
    "        1. 全字母\n",
    "        1. 字母,数字混合,且长度不一\n",
    "    1. 1.2.3ab\n",
    "    \n",
    "1. 版本号命名规则按照: https://zh.wikipedia.org/wiki/%E8%BB%9F%E4%BB%B6%E7%89%88%E6%9C%AC%E8%99%9F    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思想\n",
    "1. 提取'.'隔开的字符串:\n",
    "    1. 能转数字转数字比较\n",
    "    1. 字符比较(拆散比较ascii)\n",
    "1. 输入:两个字符串\n",
    "1. 输出:返回大者"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "def version_compare(v1,v2):\n",
    "    def icompare(item1,item2):\n",
    "        if item1.isnumeric() and item2.isnumeric():\n",
    "            diff = int(item1) - int(item2)\n",
    "            if diff == 0:                \n",
    "                return 'equal'\n",
    "            elif diff < 0:\n",
    "                return 'less'\n",
    "            return \n",
    "        \n",
    "        i_len = min(len(item1), len(item2))\n",
    "        for i in range(i_len):\n",
    "            diff = ord(item1[i]) - ord(item2[i])\n",
    "            if diff == 0:\n",
    "                continue\n",
    "            elif diff < 0:\n",
    "                return 'less'  \n",
    "        if len(item1) <len(item2):\n",
    "            return 'less'\n",
    "            \n",
    "    v1_list = v1.split('.')\n",
    "    v2_list = v2.split('.')    \n",
    "    v_len = min(len(v1_list),len(v2_list))\n",
    "    \n",
    "    for i in range(v_len):\n",
    "        res = icompare(v1_list[i],v2_list[i])\n",
    "        if res == 'equal':\n",
    "            continue\n",
    "        elif res == 'less':\n",
    "            return v2\n",
    "        else:\n",
    "            return v1    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "assert version_compare('1.2.3.a','1.2.3.b') == '1.2.3.b'\n",
    "assert version_compare('1.2.13.a','1.2.3.b') == '1.2.13.a'\n",
    "assert version_compare('1.12.13.a','1.2.3.b') == '1.12.13.a'\n",
    "assert version_compare('1.12.13.a','1.2.2.3.b') == '1.12.13.a'\n",
    "assert version_compare('1.12.13.a','1a.12.13.a') == '1a.12.13.a'"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "目录",
   "title_sidebar": "大纲",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
